// pages/apply/apply.js
const app = getApp()
Page({
  data: {
    requestUrl: app.globalData.requestUrl,
    nations: [
      "汉族", "壮族", "满族", "回族", "苗族", "维吾尔族", "土家族", "彝族", "蒙古族", "藏族",
      "布依族", "侗族", "瑶族", "朝鲜族", "白族", "哈尼族", "哈萨克族", "黎族", "傣族", "畲族",
      "傈僳族", "仡佬族", "东乡族", "高山族", "拉祜族", "水族", "佤族", "纳西族", "羌族", "土族",
      "仫佬族", "锡伯族", "柯尔克孜族", "达斡尔族", "景颇族", "毛南族", "撒拉族", "布朗族", "塔吉克族",
      "阿昌族", "普米族", "鄂温克族", "怒族", "京族", "基诺族", "德昂族", "保安族", "俄罗斯族", 
      "裕固族", "乌孜别克族", "门巴族", "鄂伦春族", "独龙族", "塔塔尔族", "赫哲族", "珞巴族", "基诺族"
    ],
    educations: ['本科', '硕士', '博士', '其他'],


    name:'',//姓名
    gender:'',//性别
    birthdate: '',//出生日期
    phoneNum:'',//联系电话
    nationIndex:0,//民族下标选项
    eduLevelIndex:0,//学历下标选项
    employer:'',//工作单位
    applyDate: '',//申请日期
    applyMotive:'',//入党动机
    selfIntro:'',//自我介绍
    handwrittenDoc:'',//手写图片


  },
  nameInput(e){
    this.setData({
      name:e.detail.value
    });
  },
  onGenderChange(e){
    this.setData({
      gender:e.detail.value
    });
  },
  birthdateChange(e){
    this.setData({
      birthdate:e.detail.value
    });
  },
  phoneNumInput(e){
    this.setData({
      phoneNum:e.detail.value
    });
  },
  nationChange(e){
    this.setData({
      nationIndex:e.detail.value
    });
  },
  educationChange(e){
    this.setData({
      eduLevelIndex:e.detail.value
    });
  },
  employerInput(e){
    this.setData({
      employer:e.detail.value
    });
  },
  applyDateChange(e){
    this.setData({
      applyDate:e.detail.value
    });
  },
  applyMotiveInput(e){
    this.setData({
      applyMotive:e.detail.value
    });
  },
  selfIntroInput(e){
    this.setData({
      selfIntro:e.detail.value
    });
  },
  // 选择图片
  chooseImage() {
    let that=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: (res) => {
        wx.uploadFile({
          url: app.globalData.requestUrl + '/user/uploadImage', //接受图片的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          success(res1) {
            let returnData = JSON.parse(res1.data);
            let imgUrl =app.globalData.requestUrl+ returnData.imgUrl;
            that.setData({
              handwrittenDoc: imgUrl
            })
          }
        })
        
      }
    })
  },

  // 表单提交
  formSubmit(e) {
    
    let that = this;
    if(that.data.name==''){
      wx.showToast({ title: '请填写姓名', icon: 'none' })
      return;
    }
    if(that.data.gender==''){
      wx.showToast({ title: '请选择性别', icon: 'none' })
      return;
    }
    if(that.data.birthdate==''){
      wx.showToast({ title: '请选择出生日期', icon: 'none' })
      return;
    }
    if(that.data.phoneNum==''){
      wx.showToast({ title: '请填写联系电话', icon: 'none' })
      return;
    }
    if(that.data.employer==''){
      wx.showToast({ title: '请填写工作单位', icon: 'none' })
      return;
    }
    if(that.data.applyDate==''){
      wx.showToast({ title: '请选择申请日期', icon: 'none' })
      return;
    }
    if(that.data.applyMotive==''){
      wx.showToast({ title: '请填写入党动机', icon: 'none' })
      return;
    }
    if(that.data.selfIntro==''){
      wx.showToast({ title: '请填写自我介绍', icon: 'none' })
      return;
    }
    if(that.data.handwrittenDoc==''){
      wx.showToast({ title: '请上传手写申请书图片', icon: 'none' })
      return;
    }

    let param = {
      name:that.data.name,
      gender:that.data.gender,
      birthdate:that.data.birthdate,
      phoneNum:that.data.phoneNum,
      nation:that.data.nations[that.data.nationIndex],
      eduLevel:that.data.educations[that.data.eduLevelIndex],
      employer:that.data.employer,
      applyDate:that.data.applyDate,
      applyMotive:that.data.applyMotive,
      selfIntro:that.data.selfIntro,
      handwrittenDoc:that.data.handwrittenDoc,
      userId:wx.getStorageSync('user').id
    };
    wx.request({
      url: app.globalData.requestUrl + '/party/application/submit',
      method: 'post',
      data: param,
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if(res.data.returnCode==200){
          wx.showToast({
            title: '提交成功',
            icon: 'success'
          })
        }else{
          wx.showToast({
            title: '请勿重复提交',
            icon: 'error'
          })
        }
      }
    });
   
  },

})